package org.shiro.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.shiro.basic.entity.Login;
import org.shiro.service.ApproveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static org.shiro.basic.constant.ShiroConstant.SUCCESS;

/**
 * @author LiKun
 * @date 2021/11/3 10:31
 */
@RestController
@RequestMapping("/approve")
@Api(tags = "系统认证相关网络接口")
public class ApproveController {
    @Resource
    private ApproveService approveService;

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public String login(@RequestBody Login login) {
        approveService.login(login);
        return String.valueOf(SecurityUtils.getSubject().getSession().getId());
    }

    @RequiresAuthentication
    @ApiOperation("用户注销接口")
    @PostMapping("/logout/{sessionId}")
    public String logout(@PathVariable("sessionId") String sessionId) {
        approveService.logout(sessionId);
        return SUCCESS;
    }
}
